package com.service.impl;

import com.dao.DeviceBoxMapper;
import com.dao.ProductMapper;
import com.dao.VenderDeviceMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.model.DeviceBox;
import com.model.Product;
import com.model.VenderDevice;
import com.page.GenericPage;
import com.po.ProductModifyPO;
import com.po.ProductPO;
import com.po.ProductQueryPO;
import com.result.Result;
import com.service.ProductService;
import com.vo.ProductQueryBySnVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.*;

/**
 * @Author: chihaojie
 * @Date: 2019/5/29 14:53
 * @Version 1.0
 * @Note
 */

@Slf4j
@Service
public class ProductServiceImpl implements ProductService {


    @Autowired
    private ProductMapper productMapper;


    @Autowired
    private VenderDeviceMapper venderDeviceMapper;

    @Autowired
    private DeviceBoxMapper deviceBoxMapper;



    @Override
    public Result<Product> save(ProductPO productPO) {

        if (StringUtils.isEmpty(productPO.getProductName())){
            return new Result<>().error("productName is null");
        }

//        商品的类别：1、售卖商品，2：陪护床

        if (StringUtils.isEmpty(productPO.getProductType())){
            return new Result<>().error("productType is null");
        }

        if (productPO.getProductType() == 1){
            //售卖
            if (StringUtils.isEmpty(productPO.getProductPrice())){
                return new Result<>().error("productPrice is null");
            }
        }else if (productPO.getProductType() ==2){
            //租赁
            if (!ObjectUtils.isEmpty(productPO.getRentPricePerDay()) && !ObjectUtils.isEmpty(productPO.getRentPricePerHour())
                    && productPO.getRentPricePerDay().doubleValue()!=0 && productPO.getRentPricePerHour().doubleValue()!=0){
                return new Result<>().error("租赁商品请区分按时租和按天租");
            }else if (productPO.getRentPricePerDay()!=null&& productPO.getRentPricePerDay().doubleValue()!=0){
                productPO.setRentPricePerHour(new BigDecimal(0));
            }else if (productPO.getRentPricePerHour()!=null&&productPO.getRentPricePerHour().doubleValue()!=0){
                productPO.setRentPricePerDay(new BigDecimal(0));
            }else{
                return new Result<>().error("参数异常");
            }
        }else{
            return new Result<>().error("productPrice is unknow");
        }

        Product product = new Product();
        BeanUtils.copyProperties(productPO,product);




        Date time = new Date();
        product.setCreateTime(time);
        product.setUpdateTime(time);
        product.setUpdated(product.getCreated());

        productMapper.insertSelective(product);

        return new Result<Product>().ok(product);
    }

    @Override
    public Result<Boolean> removeById(Long id) {

        productMapper.deleteByPrimaryKey(id);

        return new Result<>().ok();
    }

    @Override
    public Result<Boolean> modifyById(ProductModifyPO productModifyPO) {
        if (StringUtils.isEmpty(productModifyPO.getId())){
            return new Result<>().error("商品Id为空");
        }
        if (productModifyPO.getProductType()==2){
            if (!ObjectUtils.isEmpty(productModifyPO.getRentPricePerDay()) && !ObjectUtils.isEmpty(productModifyPO.getRentPricePerHour())
                    && productModifyPO.getRentPricePerDay().doubleValue()!=0 && productModifyPO.getRentPricePerHour().doubleValue()!=0){
                return new Result<>().error("租赁商品请区分按时租和按天租");
            }else if (productModifyPO.getRentPricePerDay()!=null&& productModifyPO.getRentPricePerDay().doubleValue()!=0){
                productModifyPO.setRentPricePerHour(new BigDecimal(0));
            }else if (productModifyPO.getRentPricePerHour()!=null&&productModifyPO.getRentPricePerHour().doubleValue()!=0){
                productModifyPO.setRentPricePerDay(new BigDecimal(0));
            }else{
                return new Result<>().error("参数异常");
            }
        }

        Product product = new Product();
        Date time = new Date();
        BeanUtils.copyProperties(productModifyPO,product);
        product.setUpdateTime(time);

        productMapper.updateByPrimaryKeySelective(product);

        return new Result<>().ok();
    }

    @Override
    public Result<GenericPage<Product>> queryAll(ProductQueryPO po) {
        int pageNo = po.getPageNo()==0?1:po.getPageNo();
        int pageSize = po.getPageSize()==0?10:po.getPageSize();
        PageHelper.startPage(pageNo, pageSize);
        Product product = new Product();
        BeanUtils.copyProperties(po,product);
        List<Product> list = productMapper.selectByCondition(product);
        PageInfo<Product> pageInfo = new PageInfo<>(list);
        GenericPage<Product> genericPage = new GenericPage<>(pageSize, pageInfo.getTotal(), list);
        return new Result<GenericPage<Product>>().ok(genericPage);
    }

    @Override
    public Result<Product> queryById(Long id) {
        Product product = productMapper.selectByPrimaryKey(id);
        return new Result<Product>().ok(product);
    }

    @Override
    public Result<List<ProductQueryBySnVo>> queryBySn(String sn) {

        VenderDevice device = new VenderDevice();
        device.setDeviceSn(sn);
        List<VenderDevice> venderDevices = venderDeviceMapper.selectByCondition(device);

        //查询设备
        if (venderDevices==null || venderDevices.size()==0){
            return new Result<>().error("设备不存在");
        }
        device = venderDevices.get(0);
        //查询仓位

        List<DeviceBox> deviceBoxes = deviceBoxMapper.selectByDeviceId(device.getId());

        Map<Long,ProductQueryBySnVo>  data = new HashMap<>();

        List<Long> productIds = new ArrayList<>();


        ProductQueryBySnVo tempVo = null;
        for (DeviceBox box : deviceBoxes){
            if (ObjectUtils.isEmpty(box.getProductId()) || box.getProductId()==0){
                continue;
            }
            if (data.containsKey(box.getProductId())){
                tempVo = data.get(box.getProductId());
                if (!ObjectUtils.isEmpty(box.getStock())){
                    tempVo.setStockCount(tempVo.getStockCount()+box.getStock());
                }
            }else{
                tempVo = new ProductQueryBySnVo();
                tempVo.setId(box.getProductId());
                tempVo.setStockCount(0);
                tempVo.setFactPrice(box.getFactPrice());
                if (!ObjectUtils.isEmpty(box.getStock())){
                    tempVo.setStockCount(box.getStock());
                }
                data.put(box.getProductId(),tempVo);
                productIds.add(box.getProductId());
            }
        }
        List<ProductQueryBySnVo> vos = new ArrayList<>();

        if (productIds.isEmpty()){
            return new Result<List<ProductQueryBySnVo>>().ok(vos);
        }
        List<Product> list = productMapper.selectByPrimaryKeys(productIds);

        ProductQueryBySnVo vo = null;
        for (Product p:list){
            vo = data.get(p.getId());
            BeanUtils.copyProperties(p,vo);
            if (ObjectUtils.isEmpty(p.getRentPricePerDay()) && ObjectUtils.isEmpty(p.getRentPricePerHour())){
                vo.setProductType((byte) 1);
            }else{
                vo.setProductType((byte) 2);
            }
            vos.add(vo);
        }
        return new Result<List<ProductQueryBySnVo>>().ok(vos);
    }
}




































